﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BAL;

namespace GrouponDesktop.AbmProveedor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.gridRoles.CellContentClick += new DataGridViewCellEventHandler(btnClick);
            CargarListado(null, null);
        }

        private void CargarListado(object sender, FormClosedEventArgs e)
        {
            ProveedorBAL proxy = new ProveedorBAL();
            gridRoles.DataSource = (from item in proxy.GetProveedores() select new { item.IntIdProveedor, item.VarRS, item.BitIsDeleted }).ToList();
        }

        public void btnClick(object sender, DataGridViewCellEventArgs e)
        {
            int selectedRowIndex = int.Parse(e.RowIndex.ToString());
            if (this.gridRoles.Columns[e.ColumnIndex].Name.ToString() == "btnDelete")
            {

                DAL.Entities.ProveedorEntity item = new DAL.Entities.ProveedorEntity();
                item.IntIdProveedor = (int)gridRoles.CurrentRow.Cells[2].Value;
                if (DeleteProveedor(item))
                {
                    CargarListado(null, null);
                }
            }
            else if (this.gridRoles.Columns[e.ColumnIndex].Name.ToString() == "btnEdit")
            {
                DAL.Entities.ProveedorEntity item = new DAL.Entities.ProveedorEntity();
                AgregarOEditarProveedor form = new AgregarOEditarProveedor((int)gridRoles.CurrentRow.Cells[2].Value);
                form.FormClosed += new FormClosedEventHandler(CargarListado);
                form.ShowDialog();
            }
            else if (this.gridRoles.Columns[e.ColumnIndex].Name.ToString() == "changePW")
            {
                DAL.Entities.ProveedorEntity item = new DAL.Entities.ProveedorEntity();
                CambiarPassword form = new CambiarPassword((int)gridRoles.CurrentRow.Cells[2].Value,true);
                form.ShowDialog();
            }
            else if (this.gridRoles.Columns[e.ColumnIndex].Name.ToString() == "changeRol")
            {
                bool estaBorrado = (bool)gridRoles.CurrentRow.Cells[4].Value;
                if (!estaBorrado)
                {
                    DAL.Entities.ProveedorEntity item = new DAL.Entities.ProveedorEntity();
                    CambiarACliente form = new CambiarACliente((int)gridRoles.CurrentRow.Cells[2].Value);
                    form.ShowDialog();
                    CargarListado(null, null);
                }
                else
                {
                    MessageBox.Show("El cliente se encuentra deshabilitado por lo tanto no puede cambiarse su rol.");
                }


                
            }
        }

        /// <summary>
        /// Funcion que borra logicamente un proveedor
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private bool DeleteProveedor(DAL.Entities.ProveedorEntity item)
        {
            if (MessageBox.Show("Esta seguro que desea borrar al proveedor " + item.VarRS + "?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //Borramos el rol
                BAL.ProveedorBAL proxy = new ProveedorBAL();
                proxy.DeleteProveedor(item);
                return true;
            }
            else
            {
                return false;
            }
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            AgregarOEditarProveedor form = new AgregarOEditarProveedor(0);
            form.FormClosed += new FormClosedEventHandler(CargarListado);
            form.ShowDialog();
        }

        private void btnFiltrar_Click(object sender, EventArgs e)
        {
            DAL.Entities.ProveedorEntity prov = new DAL.Entities.ProveedorEntity();


            if (txtCUIT.Text == string.Empty && txtEmail.Text == string.Empty && txtRazonSocial.Text == string.Empty)
            {
                CargarListado(null, null);
            }
            else
            {

                //Cargamos los datos
                prov.VarMail = txtEmail.Text;
                prov.VarCUIT = txtCUIT.Text;
                prov.VarRS = txtRazonSocial.Text;
                //Buscamos los datos de nuevo sin filtros
                ProveedorBAL proxy = new ProveedorBAL();
                gridRoles.DataSource = (from item in proxy.GetProveedoresFiltre(prov) select new { item.IntIdProveedor, item.VarRS, item.BitIsDeleted }).ToList();


            }
        }
    }
}
